<template>
  <div class="page">
    <v-pageTitle title="个人工时列表"></v-pageTitle>
    <!--工具条-->
    <v-pageToolbar align="left">
      <el-form :inline="true" :model="formSearch" class="demo-form-inline">
        <el-form-item label="查询条件">
          <el-date-picker
            v-model="formSearch.value"
            type="daterange"
            size="small"
            align="right"
            placeholder="选择日期范围"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </v-pageToolbar>

    <el-table v-loading="tableData.loading" :data="tableData.body" border style="width: 100%; margin-bottom: 10px;">
      <el-table-column prop="branch" label="部门" min-width="120">
      </el-table-column>
      <el-table-column prop="staff" label="编号" min-width="160">
      </el-table-column>
      <el-table-column prop="name" label="人员" min-width="100">
      </el-table-column>
      <el-table-column prop="workTime" label="工作时长" min-width="170">
      </el-table-column>
      <el-table-column prop="leaveTime" label="请假时长" min-width="170">
      </el-table-column>
      <el-table-column prop="sum" label="总工时" min-width="170">
      </el-table-column>
    </el-table>

    <!-- 图表 -->
    <v-pageToolbar align='left'>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="个人工时正负条形图">
        </el-form-item>
      </el-form>
    </v-pageToolbar>
    <div id="myChart" style="margin-top:20px;"></div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        formSearch:{
          value: new Date()
        },
        tableData: {
            loading: true,
            body: []
        },
        paginationTotal: 100,
        sels:[]
      }
    },
    methods: {
      onSearch(){
        console.log(formSearch)
        this.getTableData()
      },
      getTableData() {
          // ajax 查询表格数据
          this.tableData.body = [
            {
              id: 1,
              branch: '新业务部',
              staff: '260000019',
              name: '张宗昭',
              workTime: '175',
              leaveTime: '8',
              sum: '167'
            }
          ]
          // 模拟加载
          setTimeout(() => {
              this.tableData.loading = false
          }, 1000)
      },
      drawLine(){
        // 基于准备好的dom
        var mainContainer = document.getElementById('myChart');
        //用于使chart自适应高度和宽度,通过窗体高宽计算容器高宽
        var resizeMainContainer = function () {
            mainContainer.style.width = window.innerWidth*0.85+'px';
            mainContainer.style.height = window.innerHeight*0.5+'px';
        };
        //设置div容器高宽
        resizeMainContainer();
        // 初始化echarts实例
        var myChart = this.$echarts.init(mainContainer);  
        // 绘制图表
        myChart.setOption({
            tooltip : {
                trigger: 'axis',
                axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                    type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            legend: {
                data:['总工时', '请假时长', '工作时长']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis : [
                {
                    type : 'value',
                    axisLabel: {
                        formatter: '{value} h'
                    },
                    splitNumber: 20
                }
            ],
            yAxis : [
                {
                    type : 'category',
                    axisTick : {show: false},
                    data : ['张宗昭']
                }
            ],
            series : [
                {
                    name:'总工时',
                    type:'bar',
                    label: {
                        normal: {
                            show: true,
                            position: 'inside'
                        }
                    },
                    data:[167]
                },
                {
                    name:'工作时长',
                    type:'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true
                        }
                    },
                    data:[175]
                },
                {
                    name:'请假时长',
                    type:'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'left'
                        }
                    },
                    data:[-8]
                }
            ]
        });
        // 自适应
        window.onresize = function(){
          mainContainer.style.width = window.innerWidth*0.85+'px';
          mainContainer.style.height = window.innerHeight*0.5+'px';
          myChart.resize();
        }
      }
    },
    mounted() {
        // 获取table数据
        this.getTableData()
        // 绘制图表
        this.drawLine()
    }
  }
</script>